const initialState = {
  list: [
    { id: 1, task: '学习 vue', isDone: true },
    { id: 2, task: '学习 vue3', isDone: true },
    { id: 3, task: '学习 React', isDone: false },
  ],
};

export type ActionType = { type: 'todo/updateById'; payload: number } | { type: 'todo/clearDone' };

export default function todoReducer(state = initialState, action: ActionType): typeof initialState {
  switch (action.type) {
    case 'todo/updateById':
      return {
        ...state.list,
        list: state.list.map((item) => ({
          ...item,
          isDone: item.id === action.payload ? !item.isDone : item.isDone,
        })),
      };

    case 'todo/clearDone':
      return {
        ...state,
        list: state.list.filter((item) => !item.isDone),
      };
    default:
      return state;
  }
}
